package com.jone.phone.detail.ui;

import java.util.Calendar;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jone.phone.detail.R;
import com.jone.phone.detail.ui.chart.BarChart;
import com.jone.phone.detail.ui.chart.LineChart;
import com.jone.phone.detail.ui.chart.PieChart;

public class StatView extends Activity implements View.OnClickListener{
	
	private static final int DATE_DIALOG_ID = 1;
	
	private LinearLayout chartLayout;
	
	private TextView chartDesc;
	
	private TextView curMonth;
	
    private int mYear;
    
    private int mMonth;
    
    private String month;
    
    
    private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;             
                updateDisplay();
                //更新统计图
                getDayStat(month);                
            }
        };

        private OnClickListener header = new View.OnClickListener() {
    		
    		@Override
    		public void onClick(View v) {
    			switch(v.getId()){			
    			case R.id.tab1:
    				Intent i = new Intent(StatView.this,TodayView.class);
    				StatView.this.startActivity(i);
    				break;
    			case R.id.tab3:
    				i = new Intent(StatView.this,ConfigView.class);
    				StatView.this.startActivity(i);
    				break;    			
    			}
    			
    		}
    	};
	
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        setContentView(R.layout.chart);
	        
	        ImageView img1 = (ImageView)this.findViewById(R.id.tab1);
	        ImageView img2 = (ImageView)this.findViewById(R.id.tab2);
	        ImageView img3 = (ImageView)this.findViewById(R.id.tab3);
	        img1.setOnClickListener(header);
	        img2.setOnClickListener(header);
	        img3.setOnClickListener(header);
	        
	        chartDesc = (TextView)this.findViewById(R.id.chart_desc);
	        //设置当前显示月份
	        curMonth = (TextView)this.findViewById(R.id.cur_month);	     
	        final Calendar c = Calendar.getInstance();
	        mYear = c.get(Calendar.YEAR);
	        mMonth = c.get(Calendar.MONTH);
	        updateDisplay();
	       
	        
	        Button pieStat = (Button)this.findViewById(R.id.txt_pie);
	        Button dayStat = (Button)this.findViewById(R.id.txt_day_bar);
	        Button monthStat = (Button)this.findViewById(R.id.txt_month_bar);
	        pieStat.setOnClickListener(this);
	        dayStat.setOnClickListener(this);
	        monthStat.setOnClickListener(this);
	        
	        Button otherMonth = (Button)this.findViewById(R.id.other_month);
	        otherMonth.setOnClickListener(new View.OnClickListener() {
	            public void onClick(View v) {
	                showDialog(DATE_DIALOG_ID);
	            }
	        });
	        
	        getDayStat(month);
	 }
	 
	 private void getDayStat(String month){
		 	//设置统计图
	        chartLayout = (LinearLayout)this.findViewById(R.id.charLayout);
	        chartLayout.removeAllViews();
	        BarChart bar = new BarChart(this,"call_out","每日打电话时长");
			chartLayout.addView(bar.execute(this,month));
	 }



	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.txt_pie:
			chartDesc.setText("显示本月每天接了多少分钟电话。");
			chartLayout.removeAllViews();
			BarChart bar2 = new BarChart(this,"call_in","每日接电话时长");	
	        chartLayout.addView(bar2.execute(this,month));
	        break;
		case R.id.txt_month_bar:
			chartDesc.setText("每月通话：对该年每月打/接电话通话时长进行统计。");
			chartLayout.removeAllViews();
			LineChart line = new LineChart();
			chartLayout.addView(line.execute(this,month));
			break;
		case R.id.txt_day_bar:
			chartDesc.setText("显示本月每天打了多少分钟电话。");
			chartLayout.removeAllViews();
			BarChart bar = new BarChart(this,"call_out","每日打电话时长");
			chartLayout.addView(bar.execute(this,month));
			break;
		}
	}
	
	@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {           
            case DATE_DIALOG_ID:
                return new DatePickerDialog(this,
                            mDateSetListener,
                            mYear, mMonth, 1);
        }
        return null;
    }

    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        switch (id) {           
            case DATE_DIALOG_ID:
                ((DatePickerDialog) dialog).updateDate(mYear, mMonth, 1);
                break;
        }
    }    
	
	 private void updateDisplay() {
		 curMonth.setText(
	            new StringBuilder()
	                    // Month is 0 based so add 1
	            		.append(mYear).append("/")
	                    .append(pad(mMonth + 1)));
		 month = mYear+""+pad(mMonth + 1);
	    }
	 
	 private static String pad(int c) {
	        if (c >= 10)
	            return String.valueOf(c);
	        else
	            return "0" + String.valueOf(c);
	    }
	 
	 public void onStop(){
	    	this.finish();
	    	super.onDestroy();
	 }

}
